using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder2;

namespace UnityContrib.CanonicalObjectExtension
{
    class CanonicalObjectTypeStrategy : BuilderStrategy
    {
        // make sure context is not null
        public override void PreBuildUp(IBuilderContext context)
        {
            // make sure context is not null
            Guard.ArgumentNotNull(context, "context");

            // we only do PreBuildUp when existing object doesnt exist
            if (context.Existing != null) return;

            // partial type to build 
            Type typeToBuild;
            if (!BuildKey.TryGetType(context.BuildKey, out typeToBuild)) return;

            // get the policy from context
            IPartialObjectPolicy partialObjectPolicy = GetPartialObjectPolicy(context);


        }
        public override void PostBuildUp(IBuilderContext context)
        {
            Guard.ArgumentNotNull(context, "context");
        }

        private static IPartialObjectPolicy GetPartialObjectPolicy(IBuilderContext context)
        {
            IPartialObjectPolicy policy;
            policy = context.Policies.Get<IPartialObjectPolicy>(context.BuildKey, false);
            if (policy == null)
            {
                policy = context.Policies.Get<IPartialObjectPolicy>(BuildKey.GetType(context.BuildKey),false);
            }
            return policy;
        }

    }
}
